//
//  StoresInformationViewController.m
//  BMProject
//
//  Created by chris on 16/1/2.
//  Copyright © 2016年 BMProject. All rights reserved.
//

#import "StoresInformationViewController.h"
#import "DetailTopCell.h"
#import "InformationIdCell.h"
#import "InformationHandheldCell.h"
#import "DataModel.h"
#import "InformationChannelCell.h"
#import "InformationBranchCell.h"
@interface StoresInformationViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSDictionary *_dictionary;
    NSDictionary *websiteinfo;
    NSString *_imgStr;
    NSString *_imgStrTwo;
    NSString *_imgStrThree;
    UITableView *_tableView;
    StoresListModel * newModel;//详情接口获得的model
}

@end

@implementation StoresInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"门店信息查看";

    [self requestDetail];

}
#pragma mark-UITableViewDateSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(IsStringEmpty(self.model.auditor)){
        if([@"1" isEqualToString:websiteinfo[@"mainType"]]) {//代表个人
            return 4;
        }else{
            return 3;
        }
    }else if(IsStringEmpty(self.model.auditorSecond)){
        if([@"1" isEqualToString:websiteinfo[@"mainType"]]) {//代表个人
            return 5;
        }else{
            return 4;
        }
    }else{
        if([@"1" isEqualToString:websiteinfo[@"mainType"]]) {//代表个人
            return 6;
        }else{
            return 5;
        }
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([@"1" isEqualToString:websiteinfo[@"mainType"]]) {//代表个人
        if (indexPath.section == 0)
        {
            static NSString * cellId = @"DetailTopCell";
            DetailTopCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setInfo:_dictionary];
            cell.storesListModel = _model;
            return cell;
        }
        else if(indexPath.section == 1)
        {
            static NSString * cellId = @"InformationIdCell";
            InformationIdCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setInfo:_imgStr];
            return cell;
        }
        else if(indexPath.section == 2)
        {
            static NSString * cellId = @"InformationHandheldCell";
            InformationHandheldCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setInfo:_imgStrTwo];
            return cell;
        }
        else if(indexPath.section == 3)
        {
            static NSString * cellId = @"InformationHandheldCell";
            InformationHandheldCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_imgStrThree AndType:@"门店照片"];
            return cell;
        }
        else if(indexPath.section == 4)
        {
            static NSString * cellId = @"InformationChannelCell";
            InformationChannelCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:newModel];
            return cell;
        }
        else
        {
            static NSString * cellId = @"InformationBranchCell";
            InformationBranchCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:newModel];
            return cell;
        }
    }else{
        if (indexPath.section == 0)
        {
            static NSString * cellId = @"DetailTopCell";
            DetailTopCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setInfo:_dictionary];
            cell.storesListModel = _model;
            return cell;
        }
        else if(indexPath.section == 1)
        {
            static NSString * cellId = @"InformationHandheldCell";
            InformationHandheldCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_imgStrTwo AndType:@"营业执照扫描件"];
            return cell;
        }
        else if(indexPath.section == 2)
        {
            static NSString * cellId = @"InformationHandheldCell";
            InformationHandheldCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_imgStrTwo AndType:@"门店照片"];
            return cell;
        }
        else if(indexPath.section == 3)
        {
            static NSString * cellId = @"InformationChannelCell";
            InformationChannelCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:newModel];
            return cell;
        }
        else
        {
            static NSString * cellId = @"InformationBranchCell";
            InformationBranchCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:newModel];
            return cell;
        }
    }

}
#pragma mark- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 400;
    }else if(indexPath.section == 1 || indexPath.section == 2){
        return 250;
    }else{
        if([@"1" isEqualToString:websiteinfo[@"mainType"]] && indexPath.section == 3) {//代表个人
            return 250;
        }
        return 160;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) requestDetail {
    [UserServices getAuditListByToken:[[AppDelegate appDelegate] getUserInfo].token
                            id:_model.id
               completionBlock:^(int result, id responseObject) {
                   if (result == API_Request_Success) {
                       _dictionary = responseObject[0];

                       NSDictionary *imgMap = _dictionary[@"imgMap"];
                       websiteinfo = _dictionary[@"websiteInfo"];
                       newModel = [[StoresListModel alloc] initWithDictionary:websiteinfo error:nil];
                       if(!IsStringEmpty(imgMap)){
                           if([@"1" isEqualToString:websiteinfo[@"mainType"]]){//代表个人
                               NSDictionary *keyOne = imgMap[@"PERSONAGE_KEY_ONE"];
                               NSDictionary *keyTwo = imgMap[@"PERSONAGE_KEY_TWO"];
                               NSDictionary *keyThree = imgMap[@"LEASE_KEY"];
                               NSString *threePath = keyThree[@"fileWebPath"];
                               _imgStrThree = [BASE_IMG_URL stringByAppendingString: [NSString ignoreEmptyStr:threePath]];
                               NSString *fileWebPath = keyOne[@"fileWebPath"];
                               NSString *filePath = keyTwo[@"fileWebPath"];
                               if(!IsStringEmpty(_dictionary[@"auditor"])){
                                   newModel.auditor = _dictionary[@"auditor"];
                               }
                               if(!IsStringEmpty(_dictionary[@"auditorSecond"])){
                                   newModel.auditorSecond = _dictionary[@"auditorSecond"];
                               }
                               _imgStr = [BASE_IMG_URL stringByAppendingString: [NSString ignoreEmptyStr:fileWebPath]];
                               _imgStrTwo = [BASE_IMG_URL stringByAppendingString: [NSString ignoreEmptyStr:filePath]];
                           }else{
                               NSDictionary *keyOne = imgMap[@"COMPANY_KEY"];
                               NSDictionary *keyTwo = imgMap[@"LEASE_KEY"];
                               NSString *fileWebPath = keyOne[@"fileWebPath"];
                               NSString *filePath = keyTwo[@"fileWebPath"];
                               if(!IsStringEmpty(_dictionary[@"auditor"])){
                                   newModel.auditor = _dictionary[@"auditor"];
                               }
                               if(!IsStringEmpty(_dictionary[@"auditorSecond"])){
                                   newModel.auditorSecond = _dictionary[@"auditorSecond"];
                               }
                               _imgStr = [BASE_IMG_URL stringByAppendingString: [NSString ignoreEmptyStr:fileWebPath]];
                               _imgStrTwo = [BASE_IMG_URL stringByAppendingString: [NSString ignoreEmptyStr:filePath]];
                           }

                       }
                       _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
                       _tableView.delegate = self;
                       _tableView.dataSource = self;
                       _tableView.sectionHeaderHeight = 0;
                       _tableView.sectionFooterHeight = 0;
                       _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
                       _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 10)];
                       [self addSubview:_tableView];
                       [_tableView reloadData];
                   }
               }];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
